package com.czz.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.*;
import com.czz.common.exception.CustomerException;
import com.czz.common.exception.ResultCodeEnum;
import com.czz.vod.service.VideoService;
import com.czz.vod.utils.AliyunVodSDKUtils;
import com.czz.vod.utils.ConstantPropertiesUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: VideoServiceImpl
 * @Author Czz
 * @Date 2020/1/29
 * @Time 10:58
 **/
@Service
public class VideoServiceImpl implements VideoService {

    @Override
    public String uploadVideo(MultipartFile file) {

        String videoId = null;

        try {
            InputStream inputStream = file.getInputStream();
            String fileName = file.getOriginalFilename();
            String title = fileName.substring(0, fileName.lastIndexOf("."));

            //创建请求对象
            UploadStreamRequest request = new UploadStreamRequest(
                    ConstantPropertiesUtil.ACCESS_KEY_ID,
                    ConstantPropertiesUtil.ACCESS_KEY_SECRET,
                    title,
                    fileName,
                    inputStream
            );

            //创建文件上传器
            UploadVideoImpl uploader = new UploadVideoImpl();

            //执行文件上传，得到响应对象
            UploadStreamResponse response = uploader.uploadStream(request);

            //得到视频id
            videoId = response.getVideoId();

        } catch (IOException e) {
            throw new CustomerException(ResultCodeEnum.VIDEO_UPLOAD_ERROR);
        }

        return videoId;
    }

    @Override
    public void deleteVideo(String videoId) {
        DefaultAcsClient client = null;
        try {
            client = AliyunVodSDKUtils.initVodClient(
                    ConstantPropertiesUtil.ACCESS_KEY_ID,
                    ConstantPropertiesUtil.ACCESS_KEY_SECRET
            );
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(videoId);
            DeleteVideoResponse response = client.getAcsResponse(request);
        }
        catch (ClientException e){
            throw new CustomerException(ResultCodeEnum.VIDEO_DELETE_ERROR);
        }
    }

    @Override
    public void deleteVideoBatch(List<String> videoList) {
        DefaultAcsClient client = null;
        try {
            client = AliyunVodSDKUtils.initVodClient(
                    ConstantPropertiesUtil.ACCESS_KEY_ID,
                    ConstantPropertiesUtil.ACCESS_KEY_SECRET
            );
            DeleteVideoRequest request = new DeleteVideoRequest();

            //批量删除
            String videoId = videoList.stream().collect(Collectors.joining(","));

            request.setVideoIds(videoId);

            DeleteVideoResponse response = client.getAcsResponse(request);
        }
        catch (ClientException e){
            throw new CustomerException(ResultCodeEnum.VIDEO_DELETE_ERROR);
        }
    }

    @Override
    public CreateUploadVideoResponse getUploadAuthAndAddress(String title, String fileName) {
        try {
            //初始化
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(
                    ConstantPropertiesUtil.ACCESS_KEY_ID,
                    ConstantPropertiesUtil.ACCESS_KEY_SECRET);

            //创建请求对象
            CreateUploadVideoRequest request = new CreateUploadVideoRequest();
            request.setTitle(title);
            request.setFileName(fileName);

            //获取响应
            CreateUploadVideoResponse response = client.getAcsResponse(request);

            return response;

        } catch (ClientException e) {
            throw new CustomerException(ResultCodeEnum.VIDEO_PLAYAUTH_ERROR);
        }
    }

    @Override
    public RefreshUploadVideoResponse refreshUploadAuthAndAddress(String videoId) {
        try {
            //初始化
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(
                    ConstantPropertiesUtil.ACCESS_KEY_ID,
                    ConstantPropertiesUtil.ACCESS_KEY_SECRET);

            //创建请求对象
            RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
            request.setVideoId(videoId);

            //获取响应
            RefreshUploadVideoResponse response = client.getAcsResponse(request);

            return response;

        } catch (ClientException e) {
            throw new CustomerException(ResultCodeEnum.VIDEO_PLAYAUTH_ERROR);
        }
    }

    @Override
    public String getVideoPlayAuth(String videoId) {
        DefaultAcsClient client = null;
        try {
            client = AliyunVodSDKUtils.initVodClient(
                    ConstantPropertiesUtil.ACCESS_KEY_ID,
                    ConstantPropertiesUtil.ACCESS_KEY_SECRET);

            //请求
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(videoId);

            //响应
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);

            //得到播放凭证
            String playAuth = response.getPlayAuth();

            return playAuth;

        } catch (ClientException e) {
            throw new CustomerException(ResultCodeEnum.VIDEO_PLAYAUTH_ERROR);
        }
    }
}
